﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                while (true)
                {
                    Run();
                    Console.ResetColor();
                }
            }
            catch (Exception ex)
            {
                Console.BackgroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.BackgroundColor = ConsoleColor.White;
                Console.ResetColor();
                Run();
            }
        }

        public static void Run()
        {
            //Console.Title = "Agile 控制台程序 ";
            //Console.WriteLine("从下列列表中选择一个选项:");
            //Console.WriteLine("\t1 - 根据实体类生成数据库表");
            //Console.WriteLine("\t2 - 根据数据库表生成测试数据");
            //Console.WriteLine("\t3 - 根据视图文件(cshtml)添加页面命令数据");

            //Console.Write("Your option? ");
            //Stopwatch _sw = new Stopwatch();
            //_sw.Start();

            //switch (Console.ReadLine())
            //{
            //    case "1":
            //        CreateTableServics.Run();
            //        break;
            //    case "2":
            //        DbTestDataGenerate.Run();
            //        break;
            //    case "3":
            //        HrCollectionPageButtons.Run();
            //        break;
            //    default:
            //        Console.ForegroundColor = ConsoleColor.Red;
            //        Console.WriteLine("\n从下列列表中没有找到选项\n");
            //        Console.ForegroundColor = ConsoleColor.White;
            //        break;
            //}

            //_sw.Stop();
            //TimeSpan ts2 = _sw.Elapsed;
            //int seconds = Utils.GetObjTranNull<int>(ts2.TotalSeconds % 60);
            //int minutes = Utils.GetObjTranNull<int>((ts2.TotalSeconds / 60) % 60);
            //int hours = Utils.GetObjTranNull<int>(ts2.TotalSeconds / 3600);
            //Console.ResetColor();
            //Console.WriteLine("\n总共花费=" + string.Format("{0}:{1}:{2}", hours, minutes, seconds) + "\n");
            //Console.WriteLine(" ");

            Console.Title = "Agile 控制台程序 ";
            Console.ForegroundColor = ConsoleColor.Gray;
         
            Console.WriteLine("从下列列表中选择一个选项:");
            Dictionary<int,object> pairs = new Dictionary<int,object>();
            var types = GetAllChildClass(typeof(IStrategy));
            var instances = types.Select(startupTask => (IStrategy)Activator.CreateInstance(startupTask))
               .OrderBy(startupTask => startupTask.Sid);
            foreach(var strategy in instances) {
                Console.WriteLine("    " + strategy.Sid + "：" + strategy.Name);
                pairs.Add(strategy.Sid,strategy);
            }
            Console.Write("Your option? ");
            Stopwatch _sw = new Stopwatch();
            string input = Console.ReadLine();
            if(!string.IsNullOrWhiteSpace(input)) {
                _sw.Start();
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.WriteLine(" ");
                int key = Utils.GetObjTranNull<int>(input);
                if(pairs.ContainsKey(key)) {
                    IStrategy strategy = (IStrategy)pairs[key];
                    strategy.Run(null);
                }
            }
            _sw.Stop();
            TimeSpan ts2 = _sw.Elapsed;
            int seconds = Utils.GetObjTranNull<int>(ts2.TotalSeconds % 60);
            int minutes = Utils.GetObjTranNull<int>((ts2.TotalSeconds / 60) % 60);
            int hours = Utils.GetObjTranNull<int>(ts2.TotalSeconds / 3600);
            Console.WriteLine("\n总共花费=" + string.Format("{0}:{1}:{2}",hours,minutes,seconds) + "\n");
            Console.WriteLine(" ");
        }

        public static Type[] GetAllChildClass(Type baseType) {
            var types = AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(a => a.GetTypes().Where(t => !t.IsAbstract && t.GetInterfaces().Contains(baseType)))
            .ToArray();
            return types;
        }
    }
}
